package dk.itu.amds.auctionhouse.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import dk.itu.amds.auctionhouse.domain.AuctionHouse;

public class AuctionHouseDB {
	public static final String AUCTION_HOUSE_DB_FILE = "C:\\AuctionHouse.ah";

	private AuctionHouse auctionHouse = new AuctionHouse();
	
	public AuctionHouse getAuctionHouse() {
		return auctionHouse;
	}

	public void setAuctionHouse(AuctionHouse auctionHouse) {
		this.auctionHouse = auctionHouse;
	}



	public AuctionHouseDB() {
		File file = new File(AUCTION_HOUSE_DB_FILE);
		
		if (file.exists()) {
			try {
		        FileInputStream fis = new FileInputStream(AUCTION_HOUSE_DB_FILE);
		        ObjectInputStream ois = new ObjectInputStream(fis);
		        Object obj = ois.readObject();
		        if (obj instanceof AuctionHouse)
		            auctionHouse = (AuctionHouse) obj;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void store() {
		try {
			FileOutputStream fos = new FileOutputStream(AUCTION_HOUSE_DB_FILE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(auctionHouse);
			oos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
